// beavtweet.cpp: The Beav
// Description: a simple, lightweight application for posting to Twitter

#include <iostream>
#include <fstream>
#include <twitcurl.h>
#include <curl/curl.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "beavtweet_functions.h"
using namespace std;

int main()
{
  string keys_dir = getenv("HOME");
  int option;
  while (true)
  {
    system("clear");
    cout << "******* beavtweet *******\n\n";
    cout << "1: Post status to Twitter" << endl;
    cout << "2: Timeline" << endl;
    cout << "3: Direct Messages" << endl;
    cout << "4: Authorize beavtweet" << endl;
    cout << "5: Exit beavtweet\n\n";
    cout << "Enter selection: ";

    if (!(cin >> option))
    {
      cin.clear();
      cin.ignore(10000, '\n');
      cout << "\nInvalid input.\n";
      cin.clear();
      cin.ignore(10000, '\n');
      getchar();
    }

    else if (option > 5)
    {
      cin.clear();
      cin.ignore(10000, '\n');
      cout << "\nInvalid input. Press any key to continue...\n";
      cin.clear();
      cin.ignore(10000, '\n');
      getchar();
    }

    else if (option < 1)
    {
      cin.clear();
      cin.ignore(10000, '\n');
      cout << "\nInvalid input. Press any key to continue...\n";
      cin.clear();
      cin.ignore(10000, '\n');
      getchar();
    }

    else if (option == 1)
    {
      cin.clear();
      cin.ignore(10000, '\n');
      if (beavtweetPostStatus())
        cout << "\nTwitter status successfully updated!\n";
      else
        cout << "\nFailed to update Twitter status. May need to authorize beavtweet.\nPress any key to continue...\n";
      //cin.clear();
      //cin.ignore(10000, '\n');
      getchar();
    }

    else if (option == 2)
    {
      string web_response = beavtweetTimeline();
      system("clear");
      cout << "******************** beavtweet Timeline *************************";
      vector<string> timeline_statuses = betweenTags(web_response, "<text>", "</text>");
      vector<string> timeline_nicks = betweenTags(web_response, "<screen_name>", "</screen_name>");
      vector<string> timeline_names = betweenTags(web_response, "<name>", "</name>");
      vector<string> timeline_times = betweenTags(web_response, "<created_at>", "</created_at>");
      for(int i = ( timeline_statuses.size()-1 ); i >= 0; i--)
      {
        cout << endl << "@" << timeline_nicks[i];
        cout << " : " << timeline_names[i];
        cout << " : " << timeline_times[i];
        cout << endl << timeline_statuses[i] << endl;
      }
      cout << "\n******************** beavtweet Timeline *************************";
      cout << "\nPress SHIFT+UP/DOWN to scroll. Press RETURN to exit to main menu.";
      cin.clear();
      cin.ignore(10000, '\n');
      getchar();
    }

    else if (option == 3)
    {

      string web_response = BTGetDirectMessages();
      system("clear");
      cout << "********************* Direct Messages ***************************";
      vector<string> direct_messages = betweenTags(web_response, "<text>", "</text>");
      vector<string> dm_nicks = betweenTags(web_response, "<screen_name>", "</screen_name>");
      vector<string> dm_names = betweenTags(web_response, "<name>", "</name>");
      vector<string> dm_times = betweenTags(web_response, "<created_at>", "</created_at>");
      for(int i = ( direct_messages.size() -1 ); i >= 0; i--)
      {
        cout << endl << "@" << dm_nicks[i];
        cout << " : " << dm_names[i];
        cout << " : " << dm_times[i];
        cout << endl << direct_messages[i] << endl;
      }
      cout << "\n********************* Direct Messages ***************************";
      cout << "\nPress SHIFT+UP/DOWN to scroll. Press RETURN to exit to main menu.";
      cin.clear();
      cin.ignore(10000, '\n');
      getchar();
    }

    else if (option == 4)
    {
      beavtweetAuthorize();
      cout << "\nYou have now completed the authorization process.\n";
      cin.clear();
      cin.ignore(10000, '\n');
      getchar();
    }

    else if (option == 5)
    {
      cin.clear();
      cin.ignore(10000, '\n');
      cout << "\nThank you for using beavtweet!\n";
      clock_t goal = 3000 + clock();
      while (goal > clock());
      break;
    }

  }

}
